<?php

namespace app\api\controller;

use think\Request;
use think\facade\Db;
use think\Response;

class User extends Wap
{
    /**
     * 获取用户信息
     * @param Request $request
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function userInfo(Request $request)
    {
        if (request()->isPost()) {

            $user = $this->checkToken();

            if ($user) {
                $user = Db::name('member')->where('id', $user['id'])->find();

                if ($user['avatar']) {
                    if (preg_match("/^(http:\/\/|https:\/\/).*$/", $user['avatar'])) {
                        $user['avatar_url'] = $user['avatar'];
                    } else {
                        $user['avatar_url'] = get_image_url($user['avatar']);
                    }
                } else {
                    $user['avatar_url'] = '';
                }
                $coupon = Db::name('mini_user_coupon')->where('status', '=', 0)->where('mark', 1)->where('user_id', $user['id'])->select();
                $r = 0;
                foreach ($coupon as $i => $v) {
                    $cradinfo = Db::name('coupon')->where('id', $v['coupon_id'])->find();
                    if ($cradinfo) {
                        $r += 1;
                    }
                }
                $pr = Db::name('mini_user_pricecoupon')->where('order_on','<>',null)->where('status', '=', 0)->where('mark', 1)->where('user_id', $user['id'])->count();
                $car = Db::name('mini_user_cardcoupon')->where('order_on','<>',null)->where('status', '=', 0)->where('mark', 1)->where('user_id', $user['id'])->count();

                if ($user['member_level'] > 0) {
                    $userlevel = Db::name('member_level')->where('id', $user['member_level'])->find();
                }
                $data['user'] = array();
                $data = array(
                    'id' => $user['id'],
                    'avatar' => $user['avatar_url'] ? $user['avatar_url'] : '',
                    'nickname' => $user['nickname'] ? $user['nickname'] : '',
                    'mobile' => $user['mobile'] ? $user['mobile'] : '',
                    'gender' => $user['gender'],
                    'balance' => $user['balance'],
                    'coupon' => $r,
                    'pricecoupon' => $pr,
                    'cardcoupon' => $car,
                    'integral' => $user['integral'],    //可用积分
                    'level_name' => isset($userlevel['name']) ? $userlevel['name'] : '',
                );
                $arr['code'] = 200;
                $arr['data'] = $data;
                $arr['message'] = '获取成功';
                Response::create($arr, 'json')->send();
                exit();
            }
        }

    }


    /**
     * 余额明细
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function balance(Request $request)
    {
        if (request()->isPost()) {
            $user = $this->checkToken();

            $data = array();
            $list = Db::name('customer_user_balance')->where('user_id', $user['id'])->select();
            foreach ($list as $i => $v) {
                $data[] = array(
                    'content' => '充值金额',
                    'price' => $v['price'],
                    'type' => $v['type'],
                    'create_time' => date('Y.m.d H:i:s', $v['create_time']),
                );
            }

            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }


    /**
     * 积分明细
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function integralList(Request $request)
    {
        if (request()->isPost()) {
            $user = $this->checkToken();

            $list = Db::name('mini_user_integral')->where('user_id', $user['id'])->select();
            $data = array();
            foreach ($list as $i => $v) {
                switch ($v['type']) {
                    case 1:
                        $goods_id = Db::name('customer_integral_product')->where('id', $v['goods_id'])->find();
                        $v['name'] = "兑换积分商品" . '-' . $goods_id['name'];
                        $status = 1;
                        break;
                    case 2:
                        $goods_id = Db::name('customer_course')->where('id', $v['goods_id'])->find();
                        $status = 1;
                        $v['name'] = " 预约包厢所得" . '-' . $goods_id['name'];
                        break;
                    case 3:
                        $status = 2;
                        $goods_id = Db::name('customer_integral_product')->where('id', $v['goods_id'])->find();
                        $v['name'] = "积分商品退款" . '-' . $goods_id['name'];


                        break;
                }
                $data[] = array(
                    'name' => $v['name'],
                    'integral' => $v['integral'],
                    'create_time' => date('Y--m-d H:i:s'),
                    'type' => $status,
                );
            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }

}